package org.genntii.warehousesystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.Exception.PasswordErrorException;
import org.genntii.warehousesystem.Exception.UsernameExistsException;
import org.genntii.warehousesystem.Exception.UsernameNotFoundException;
import org.genntii.warehousesystem.common.JwtProperties;
import org.genntii.warehousesystem.domain.dto.EmployeeDTO;
import org.genntii.warehousesystem.domain.dto.EmployeeRegisterDTO;
import org.genntii.warehousesystem.domain.dto.LoginDTO;
import org.genntii.warehousesystem.domain.entity.Employee;
import org.genntii.warehousesystem.mapper.EmployeeMapper;
import org.genntii.warehousesystem.security.DBEmployeeManager;
import org.genntii.warehousesystem.service.EmployeeService;
import org.genntii.warehousesystem.utils.JwtUtil;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private DBEmployeeManager employeeManager;

    @Override
    public void register(EmployeeRegisterDTO employeeRegisterDTO) {
        if (employeeMapper.usernameIsExist(employeeRegisterDTO.getUsername()) >= 1){
            throw new UsernameExistsException("用户名已经存在");
        }

        Employee employee = new Employee();
        String salt = BCrypt.gensalt();
        Boolean married;
        if (employeeRegisterDTO.getMarried() == 0){
            married = Boolean.FALSE;
        }else {
            married = Boolean.TRUE;
        }
        employee.setUsername(employeeRegisterDTO.getUsername());
        employee.setPassword(BCrypt.hashpw(employeeRegisterDTO.getPassword(),salt));
        employee.setSalt(salt);
        employee.setName(employeeRegisterDTO.getName());
        employee.setSex(employeeRegisterDTO.getSex());
        employee.setMarried(married);
        employee.setEducation(employeeRegisterDTO.getEducation());
        employee.setTel(employeeRegisterDTO.getTel());
        employee.setAddress(employeeRegisterDTO.getAddress());
        employee.setHiredate(LocalDate.now());
        employee.setStatus(1);
        employeeMapper.insert(employee);

    }

    @Override
    public Map<Object, Object> login(LoginDTO loginDTO) {
        try {
            employeeMapper.usernameIsExist(loginDTO.getUsername());
        }catch (Exception e){
            throw new UsernameNotFoundException("用户名不存在");
        }

        if (!BCrypt.checkpw(loginDTO.getPassword(),employeeMapper.selectPassword(loginDTO.getUsername()))){
            throw new PasswordErrorException("密码错误");
        }

        Map<String ,Object> claims = Map.of("id",employeeMapper.selectIdByUsername(loginDTO.getUsername()),"username",loginDTO.getUsername());

        String token = JwtUtil.createJWT(jwtProperties.getEmployeeSecretKey(),jwtProperties.getEmployeeTtl(),claims );

        return Map.of("message",employeeManager.loadUserByUsername(loginDTO.getUsername()),"token",token);
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {

        Boolean married;
        if (employeeDTO.getMarried()==0){
            married = Boolean.FALSE;
        }else {
            married = Boolean.TRUE;
        }

        employeeMapper.employeeUpdate(
                employeeDTO.getName(),
                married,
                employeeDTO.getEducation(),
                employeeDTO.getTel(),
                employeeDTO.getAddress(),
                employeeDTO.getStatus(),
                employeeDTO.getId());
    }

    @Override
    public void term(int id) {
        employeeMapper.term(LocalDate.now(),id);
    }
}
